import machine
import utime

# 定义引脚和对应的频率
pwm_pin = machine.Pin(1)
pwm = machine.PWM(pwm_pin)

freq_map = {
    1: 262,
    2: 294,
    3: 330,
    4: 349,
    5: 392,
    6: 440,
    7: 494,
}

# "小星星" 的数字简谱
notes = [
    {'num': 1, 'duration': 1},
    {'num': 1, 'duration': 1},
    {'num': 5, 'duration': 1},
    {'num': 5, 'duration': 1},
    {'num': 6, 'duration': 1},
    {'num': 6, 'duration': 1},
    {'num': 5, 'duration': 2},

    {'num': 4, 'duration': 1},
    {'num': 4, 'duration': 1},
    {'num': 3, 'duration': 1},
    {'num': 3, 'duration': 1},
    {'num': 2, 'duration': 1},
    {'num': 2, 'duration': 1},
    {'num': 1, 'duration': 2},

    {'num': 5, 'duration': 1},
    {'num': 5, 'duration': 1},
    {'num': 4, 'duration': 1},
    {'num': 4, 'duration': 1},
    {'num': 3, 'duration': 1},
    {'num': 3, 'duration': 1},
    {'num': 2, 'duration': 2},

    {'num': 5, 'duration': 1},
    {'num': 5, 'duration': 1},
    {'num': 4, 'duration': 1},
    {'num': 4, 'duration': 1},
    {'num': 3, 'duration': 1},
    {'num': 3, 'duration': 1},
    {'num': 2, 'duration': 2},

    {'num': 1, 'duration': 1},
    {'num': 1, 'duration': 1},
    {'num': 5, 'duration': 1},
    {'num': 5, 'duration': 1},
    {'num': 6, 'duration': 1},
    {'num': 6, 'duration': 1},
    {'num': 5, 'duration': 2},

    {'num': 4, 'duration': 1},
    {'num': 4, 'duration': 1},
    {'num': 3, 'duration': 1},
    {'num': 3, 'duration': 1},
    {'num': 2, 'duration': 1},
    {'num': 2, 'duration': 1},
    {'num': 1, 'duration': 2},
]

def play_littlestar():
    # 播放每个音符
    for note in notes:
        freq = freq_map[note['num']]
        pwm.freq(freq)
        duty = 32768
        pwm.duty_u16(duty)
        utime.sleep(note['duration'] * 0.5)
        duty = 0
        pwm.duty_u16(duty)
        utime.sleep(0.1)

while True:
    play_littlestar()